//
//  PersistenceData.m
//  YueMi
//
//  Created by Rainbow on 4/27/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "PersistenceData.h"

#define SAVED_USER          @"SAVED_USER"
#define SAVED_SHOP          @"SAVED_SHOP"
#define HISTORY_SHOP          @"HISTORY_SHOP"

@implementation PersistenceData

static PersistenceData *_sharedInst;

+(PersistenceData *)sharedData
{
	@synchronized(self)
	{
		if (_sharedInst == nil) {
			_sharedInst = [[PersistenceData alloc] init];
		}
	}
	return _sharedInst;
}	


- (void)saveUser:(User *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:SAVED_USER] != nil) {
        [defaults removeObjectForKey:SAVED_USER];
    }
    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
    [defaults setObject:userData forKey:SAVED_USER];
    [defaults synchronize];
}

- (User *)getAllUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *userData = [defaults objectForKey:SAVED_USER];
    if (userData == nil) {
        return nil;
    }
	User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:userData];
	if (user != nil) {
        return user;
	}
    return nil;
}

- (void)deleteUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:SAVED_USER] != nil) {
        [defaults removeObjectForKey:SAVED_USER];
    }
}


- (BOOL)addShop:(Shop *)shop
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *shopData = [defaults objectForKey:SAVED_SHOP];
    if (shopData == nil) {
        NSMutableArray *shops = [[[NSMutableArray alloc] initWithObjects:shop, nil] autorelease];
        shopData = [NSKeyedArchiver archivedDataWithRootObject:shops];
    }
    else
    {
        NSMutableArray *shops = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:shopData];
        for (Shop *enumshop in shops) {
            if ([shop.shopId isEqualToString:enumshop.shopId]) {
                return NO;
            }
        }
        [shops addObject:shop];
        shopData = [NSKeyedArchiver archivedDataWithRootObject:shops];
    }
    [defaults setObject:shopData forKey:SAVED_SHOP];
    [defaults synchronize];
    return YES;
}

- (NSArray *)getAllShops
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *shopData = [defaults objectForKey:SAVED_SHOP];
    if (shopData == nil) {
        return nil;
    }
	NSArray *shops = (NSArray *)[NSKeyedUnarchiver unarchiveObjectWithData:shopData];
	if (shops != nil) {
        return shops;
	}
    return nil;
}

- (void)deleteShop:(NSString *)shopId
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *shopData = [defaults objectForKey:SAVED_SHOP];
    if (shopData == nil) {
        return;
    }
    NSMutableArray *shops = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:shopData];
    
    Shop *deleteShop;
    for (Shop *shop in shops) {
        if ([shopId isEqualToString:shop.shopId]) {
            deleteShop = shop;
            break;
        }
    }
    
    [shops removeObject:deleteShop];
    
    shopData = [NSKeyedArchiver archivedDataWithRootObject:shops];
    [defaults setObject:shopData forKey:SAVED_SHOP];
    [defaults synchronize];
}

- (void)deleteAllShop
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:SAVED_SHOP] != nil) {
        [defaults removeObjectForKey:SAVED_SHOP];
    }
}


- (void)addHistoryShop:(Shop *)shop
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *shopData = [defaults objectForKey:HISTORY_SHOP];
    if (shopData == nil) {
        NSMutableArray *shops = [[[NSMutableArray alloc] initWithObjects:shop, nil] autorelease];
        shopData = [NSKeyedArchiver archivedDataWithRootObject:shops];
    }
    else
    {
        NSMutableArray *shops = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:shopData];
        for (Shop *enumshop in shops) {
            if ([shop.shopId isEqualToString:enumshop.shopId]) {
                return;
            }
        }
        
        [shops addObject:shop];
        if ([shops count] > 10) {
            Shop *oldestShop = [shops objectAtIndex:0];
            [shops removeObject:oldestShop];
        }
        
        shopData = [NSKeyedArchiver archivedDataWithRootObject:shops];
    }
    [defaults setObject:shopData forKey:HISTORY_SHOP];
    [defaults synchronize];
}

- (NSArray *)getAllHistoryShops
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *shopData = [defaults objectForKey:HISTORY_SHOP];
    if (shopData == nil) {
        return nil;
    }
	NSArray *shops = (NSArray *)[NSKeyedUnarchiver unarchiveObjectWithData:shopData];
	if (shops != nil) {
        return shops;
	}
    return nil;
}

- (void)deleteAllHistoryShop
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:HISTORY_SHOP] != nil) {
        [defaults removeObjectForKey:HISTORY_SHOP];
    }
}

@end
